package com.sunlands.deskmate.web;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sunlands.deskmate.client.DeskMateGroupService;
import com.sunlands.deskmate.client.UserCenterService;
import com.sunlands.deskmate.domain.TzChatRecord;
import com.sunlands.deskmate.repository.TzChatRecordRepository;
import com.sunlands.deskmate.service.MessageService;
import com.sunlands.deskmate.vo.GroupSimpleInfo;
import com.sunlands.deskmate.vo.Message;
import com.sunlands.deskmate.vo.UsersVO;
import com.sunlands.deskmate.vo.response.BusinessResult;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by shixi  on 2019/11/4
 */
@SuppressWarnings("unused")
@Api(tags = "清洗数据")
@Slf4j
@RestController
public class DataController {

    @Autowired
    private MessageService messageService;

    @Autowired
    private TzChatRecordRepository tzChatRecordRepository;
    @Autowired
    private DeskMateGroupService deskMateGroupService;
    @Autowired
    private UserCenterService tzUserCenterService;


    @GetMapping("/clean")
    public BusinessResult<List<Message>> cleanMessageList() {
        log.info("清洗数据");
        messageService.cleanData();
        return BusinessResult.createSuccessInstance(null);
    }

    @GetMapping("/clean-type")
    public BusinessResult<List<Message>> cleanType(@RequestParam String type) {
        log.info("清洗 {} 数据", type);
        List<TzChatRecord> chatRecords = tzChatRecordRepository.findBySql(type);
        log.info("需要清洗的条数{}", chatRecords.size());
        ObjectMapper mapper = new ObjectMapper();
        for (TzChatRecord tzChatRecord : chatRecords) {
            String contentId = tzChatRecord.getContentId();
            //请求群组信息
            GroupSimpleInfo groupSimpleInfo = deskMateGroupService.getGroupSimpleInfo(contentId).getData();
            //请求个人信息
            UsersVO usersVO = tzUserCenterService.getUserInfoById(Long.parseLong(tzChatRecord.getFromUserId())).getData();

            Map<String, String> extras = new HashMap<>();

            extras.put("groupType", groupSimpleInfo.getType().toString());
            extras.put("name", groupSimpleInfo.getGroupName());
            extras.put("avatar", groupSimpleInfo.getAvatarFile());
            extras.put("content", groupSimpleInfo.getGroupDesc());
            extras.put("fromAvatar", usersVO.getAvatarUrl());
            try {
                tzChatRecord.setExtras(mapper.writeValueAsString(extras));
            } catch (JsonProcessingException e) {
                log.info("extras writeValueAsString error");
            }

            tzChatRecordRepository.save(tzChatRecord);
        }
        return BusinessResult.createSuccessInstance(null);
    }
}
